# -*- coding: utf-8 -*-

from django.http import HttpResponse
from django.http import JsonResponse
import json
# 引入鉴权装饰器
from vue_app.utils.auth_permission import auth_permission_require
# 引入数据模型
from vue_app.models import DeviceMgrTable
# 引入序列化器
from django.core import serializers

# 设备加载
@auth_permission_require('')
def deviceInit(request):
    # response
    response = {}
    # data
    data = json.loads(request.body)
    # 选取数据
    user = request.user
    devices = DeviceMgrTable.objects.filter(user_id=user.id).order_by("device_id")
    # 序列化
    devices_serialize = serializers.serialize("json", devices)
    # 反序列化
    devices_json = json.loads(devices_serialize)
    response.update({"devices": devices_json})
    response.update({"code": 20000})
    return JsonResponse(response)

# 设备添加
@auth_permission_require('')
def deviceAdd(request):
    # response
    response = {}
    # data
    data = json.loads(request.body)
    deviceID = data.get("deviceID", "")
    address = data.get("address", "")
    # user
    user = request.user
    user_id = user.id
    user_name = user.username
    # filter
    if DeviceMgrTable.objects.filter(user_id=user_id, device_id=deviceID).exists():
        # 存在，不可添加
        response.update({"opcode": 0})
        pass
    else:
        # 不存在
        device = DeviceMgrTable(
            user_id=user_id,
            device_id=deviceID,
            user_name=user_name,
            description=address
        )
        device.save()
        response.update({"opcode": 1, "deviceID": deviceID, "address": address, "user_id": user_id, "user_name": user_name})
        devices = DeviceMgrTable.objects.filter(user_id=user.id).order_by("device_id")
        # 序列化
        devices_serialize = serializers.serialize("json", devices)
        # 反序列化
        devices_json = json.loads(devices_serialize)
        response.update({"devices": devices_json})
        pass
    response.update({"code": 20000})
    return JsonResponse(response)

# 设备删除
@auth_permission_require('')
def deviceDel(request):
    # response
    response = {}
    # data
    data = json.loads(request.body)
    deviceID = data.get("deviceID", "")
    # user
    user = request.user
    user_id = user.id
    # filter
    if DeviceMgrTable.objects.filter(user_id=user_id, device_id=deviceID).exists():
        # 存在，可以删除
        DeviceMgrTable.objects.filter(user_id=user_id, device_id=deviceID).delete()
        response.update({"opcode": 1})
        pass
    else:
        # 不存在，不可删除
        response.update({"opcode": 0})
        pass
    # 全量发送
    devices = DeviceMgrTable.objects.filter(user_id=user_id).order_by("device_id")
    # 序列化
    devices_serialize = serializers.serialize("json", devices)
    # 反序列化
    devices_json = json.loads(devices_serialize)
    response.update({"devices": devices_json})

    response.update({"code": 20000})
    return JsonResponse(response)

# 设备修改
@auth_permission_require('')
def deviceEdit(request):
    # response
    response = {}
    # data
    data = json.loads(request.body)
    deviceID = data.get("deviceID", "")
    description = data.get("description")
    # user
    user = request.user
    user_id = user.id
    # filter
    if DeviceMgrTable.objects.filter(user_id=user_id, device_id=deviceID).exists():
        # 存在，可以修改
        DeviceMgrTable.objects.filter(user_id=user_id, device_id=deviceID).update(description=description)
        response.update({"opcode": 1})
        pass
    else:
        # 不存在，不可修改
        response.update({"opcode": 0})
        pass
    # 全量发送
    devices = DeviceMgrTable.objects.filter(user_id=user_id).order_by("device_id")
    # 序列化
    devices_serialize = serializers.serialize("json", devices)
    # 反序列化
    devices_json = json.loads(devices_serialize)
    response.update({"devices": devices_json})

    response.update({"code": 20000})
    return JsonResponse(response)
